package com.example.upimageadapter.other;


import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 用来完成RecyclerView长按拖拽的关键接口
 * 1.getMovementFlags里面表示设置为上下拖动
 * 2.onSelectedChanged里面表示拖动状态下改变背景色，拖动完成后恢复背景色
 * 3.拖动完成的时候viewHolder的值为空！！！所以要用srcHolder
 */
public class MyItemTouchHelperCallback extends ItemTouchHelper.Callback {

    private IItemTouchHelperAdapter mAdapter;

    public MyItemTouchHelperCallback(IItemTouchHelperAdapter mAdapter) {
        this.mAdapter = mAdapter;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {

        //上下拖拽，若有其他需求同理
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        //向右侧滑，若有其他需求同理
        int swipeFlags = ItemTouchHelper.RIGHT;
        return makeMovementFlags(dragFlags, swipeFlags);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder srcHolder, RecyclerView.ViewHolder targetHolder) {
        //通知Adapter更新数据和视图
        mAdapter.onItemMove(srcHolder.getAdapterPosition(), targetHolder.getAdapterPosition());
        //若返回false则表示不支持上下拖拽
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        //通知Adapter更新数据和视图
        mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        //是否可以左右侧滑，默认返回true
        return false;
    }

    @Override
    public boolean isLongPressDragEnabled() {
        //是否可以长按上下拖拽，默认返回false
        return false;
    }

    public interface OnStartDragListener {
        /**
         * 当View需要拖拽时回调
         *
         * @param viewHolder The holder of view to drag
         */
        void onStartDrag(RecyclerView.ViewHolder viewHolder);
    }
}